﻿using LD.BC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace LD.Vision
{
    public class CameraBrandHelper
    {
        public List<CameraModel> GetList()
        {
            List<CameraModel> cameraList = new List<CameraModel>();

            var c_class = GetClasses();
            foreach (var item in c_class)
            {
                CameraAttribute bcAttribute = (CameraAttribute)item.GetCustomAttribute(typeof(CameraAttribute), true);
                if (bcAttribute != null)
                {
                    cameraList.Add(new CameraModel()
                    {
                        Name = item.Name,
                        CameraType = bcAttribute.Brand,
                    });
                }
            }
            return cameraList;
        }
        private IEnumerable<Type> GetClasses()
        {
            //反射
            var typess = Assembly.GetCallingAssembly().GetTypes()
                        .Where(p => p.GetInterfaces().Contains(typeof(AbsCamera))).ToList();
            return typess;
        }
        public AbsCamera GetCameraInstance(string className)
        {
            AbsCamera camera = null;
            //反射
            Type type = Type.GetType($"LD.Vision.{className}");
            if (type != null)
                camera = (AbsCamera)type.Assembly.CreateInstance($"LD.Vision.{className}");
            //var typess = Assembly.GetCallingAssembly().GetTypes()
            //    .Where(p => p.BaseType == typeof(IBoardCard));
            //var assembly = Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LD.BC.dll"));
            //var bc = (IBoardCard)assembly.CreateInstance($"LD.BC.{ className}", true);
            return camera;
        }
    }
}
